package com.taptech.doufu.ui.fragment.story

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.taptech.doufu.R
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.model.novel.bean.NovelDetailBean
import com.taptech.doufu.ui.adapter.StoryAdapter
import com.taptech.doufu.ui.fragment.DiaobaoBaseFragment
import com.taptech.doufu.util.GsonUtil
import com.taptech.doufu.weex.http.TFHttpUtil

class StoryMainFragment : DiaobaoBaseFragment(){

    lateinit var url: String
    val size = 20
    var page = 1
    lateinit var adapter:StoryAdapter
    lateinit var mRecyclerView: RecyclerView
    lateinit var mRefreshLayout: SmartRefreshLayout
    lateinit var layNone:View


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater?.inflate(R.layout.fragment_story_main, container, false)
        mRecyclerView = view.findViewById(R.id.mRecyclerView)
        mRefreshLayout = view.findViewById(R.id.mRefreshLayout)
        layNone = view.findViewById(R.id.layNone)
        url = arguments!![Constant.URL] as String
        adapter = StoryAdapter(context!!)
        mRecyclerView.layoutManager = LinearLayoutManager(context)
        mRecyclerView.adapter = adapter
        mRefreshLayout.setOnRefreshListener {
            getData(true)
        }
        mRefreshLayout.setOnLoadmoreListener {
            getData(false)
        }
        mRefreshLayout.autoRefresh()
        return view
    }



    fun getData(isRefresh: Boolean) {
        if (isRefresh){
            page = 1
        }
        val params = mutableMapOf<String,Any>()
        params["size"] = size
        params["page"] = page
        TFHttpUtil.requestGet(url,params){json, error ->
            try {
                val data = json.getJSONObject("data")
                page += 1
                val list = GsonUtil.jsonToList(data.getJSONArray("list").toJSONString(), Array<NovelDetailBean>::class.java)
                updateUI(isRefresh,list)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }


    fun updateUI(isRefresh: Boolean,list: MutableList<NovelDetailBean>) {
        val formatList = ArrayList(list)
        if (formatList.size < size) {
            mRefreshLayout.isEnableLoadmore = false
        }
        if (isRefresh) {
            adapter.setData(formatList)
            mRefreshLayout.finishRefresh()
        }else{
            adapter.addData(formatList)
            mRefreshLayout.finishLoadmore()
        }

        if (adapter.dataList.size > 0) {
            layNone.visibility = View.GONE
        }else{
            layNone.visibility = View.VISIBLE
        }
    }


    companion object{
        fun newInstance(url: String):StoryMainFragment{
            val fragment = StoryMainFragment()
            val bundle = Bundle()
            bundle.putString(Constant.URL,url)
            fragment.arguments = bundle
            return fragment
        }
    }

}